<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      class="login-form"
      :model="loginFrom"
      label-position="left"
    >
      <!-- :model   绑定数据
           :rules   绑定规则
       -->

      <div class="title-container">
        <h3 class="title">
          <img src="@/assets/login-logo.png" alt="">
        </h3>
      </div>
      <div style="width:96px; position: absolute; top:-48px; left:200px;"><img style="width:100%;" src="../../assets/lkdlogo.png" alt=""></div>
      <el-form-item prop="loginName">
        <span class="svg-container el-icon-user-solid" />
        <el-input
          v-model="loginFrom.loginName"
          placeholder="请输入手机号"
        />
        <!-- loginFrom.loginName   绑定到return里面loginFrom中的loginName属性里 -->
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input ref="pwdInput" v-model="loginFrom.password" :type="type" placeholder="请输入密码" />
        <span class="svg-container">
          <svg-icon :icon-class="`${type === 'password' ? 'eye' : 'eye-open'}`" @click="checkPwd" />
        </span>
      </el-form-item>

      <el-form-item prop="code">
        <span class="svg-container el-icon-coordinate" />
        <el-input
          v-model="loginFrom.code"
          placeholder="请输入验证码"
        /><div style="height:48px; width:130px; position: absolute; right:1px; top:-1px;" @click="Verification()"><img :src="codeSrc" alt=""></div>
      </el-form-item>

      <el-button
        type="primary"
        class="loginBtn"
        style="width:450px; height:50px; line-height:20px; font-size:14px; margin-bottom:30px; background-color:#7377ed;"
        @click="submitLogin"
      >登陆</el-button>

    </el-form>
  </div>
</template>

<script>
import { VerificationAPI, loginAPI } from '@/api/user'
export default {
  name: 'Login',
  data() {
    return {
      loginFrom: {
        loginName: '',
        password: '',
        code: '',
        clientToken: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJsa2QiLCJpYXQiOjE2Njk3MjU0MTYsIm1vYmlsZSI6bnVsbCwidXNlcklkIjoxLCJsb2dpblR5cGUiOjAsImV4cCI6MTY2OTcyNTQxNn0.ifP7SGyoTcQP9QUvznn1fqeMzp1eneNvve4dT3sTHLU',
        loginType: 0
      },
      codeSrc: '',
      type: 'password'
    }
  },
  created() {
    this.Verification()
  },
  methods: {
    checkPwd() {
      this.type === 'password' ? this.type = '' : this.type = 'password'
      this.$nextTick(() => {
        this.$refs.pwdInput.focus()
      })
    },
    async submitLogin() {
    //   this.$refs.loginForm.validate((vali) => {
    //     if (vali) {
    //       // console.log(vali)
    //       this.$store.dispatch('user/loginAction', this.loginFrom)
    //       router.push('/')
    //     }
    //   })
      const res = await loginAPI(this.loginFrom)
      console.log(res)
      if (res.data.msg === '登录成功') {
        this.$store.dispatch('user/loginAction', this.loginFrom)
        this.$router.push('/home')
      }
    },
    async Verification() {
      const res = await VerificationAPI(this.loginFrom.clientToken)
      // console.log(res)
      this.codeSrc = (window.URL || window.webkitURL).createObjectURL(res.data)
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#68b0fe;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid #e9e9e9;
    background: rgba(255, 255, 255, 0.7); // 输入登录表单的背景色
    border-radius: 5px;
    color: #454545;
  }
  .el-form-item__error {
    color: #fff
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#68b0fe;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  background-image: url('~@/assets/lkdbgc.jpg');
  background-position: center;

  .login-form {
    position: relative;
    background-color: #fff;
    width: 520px;
    max-width: 100%;
    padding: 0px 35px 0;
    margin: 200px auto 0;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .loginBtn {
  background: #407ffe;
  height: 64px;
  line-height: 32px;
  font-size: 24px;
}
}
</style>
